package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.CostDrugMapper;
import com.wn.mapper.CostMapper;
import com.wn.mapper.NurseRecordMapper;
import com.wn.ov.DruglistOv;
import com.wn.ov.NurseRecordAndCostDrugOv;
import com.wn.ov.NurseRecordOv;
import com.wn.pojo.Cost;
import com.wn.pojo.CostDrug;
import com.wn.pojo.Drug;
import com.wn.pojo.NurseRecord;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: his
 * @BelongsPackage: com.wn.service
 * @Author: faker
 * @CreateTime: 2020-10-26 15:48
 * @Description:
 */
@Service
@Transactional
public class NurseRecordService {

    @Autowired
    private NurseRecordMapper nurseRecordMapper;

    @Autowired
    private CostService costService;

    @Autowired
    private CostMapper costMapper;

    @Autowired
    private CostDrugMapper costDrugMapper;

    //通过护士id获取护理列表
    public ServerResponse getNursingListByDoctorId(PageParam<NurseRecord> pageParam){

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<NurseRecordOv> list = nurseRecordMapper.superSearch(pageParam.getSuperSearchKeyWord(),pageParam.getModel().getId());
            //时间显示格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH时mm分");
            for (NurseRecordOv n:list) {
                String timeString = formatter.format(n.getTime());
                n.setTimeString(timeString);
            }
            PageInfo<NurseRecordOv> nurseRecordPageInfo = new PageInfo<NurseRecordOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS,nurseRecordPageInfo);
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }

            List<NurseRecordOv> list = nurseRecordMapper.getListByNurseId(pageParam.getModel());
            //时间显示格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH时mm分");
            for (NurseRecordOv n:list) {
                String timeString = formatter.format(n.getTime());
                n.setTimeString(timeString);
            }
            PageInfo<NurseRecordOv> nurseRecordPageInfo = new PageInfo<NurseRecordOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS,nurseRecordPageInfo);
        }
    }

    //添加护理记录
    public ServerResponse addNursingRecord(NurseRecordAndCostDrugOv nurseRecordAndCostDrugOv,Integer userId){

        NurseRecord nurseRecord = new NurseRecord();
        nurseRecord.setPatientId(nurseRecordAndCostDrugOv.getPatientId());
        nurseRecord.setNurseId(userId);
        nurseRecord.setDoctorId(nurseRecordAndCostDrugOv.getDoctorId());
        nurseRecord.setType(nurseRecordAndCostDrugOv.getType());
        nurseRecord.setDetail(nurseRecordAndCostDrugOv.getDetail());
        int result = nurseRecordMapper.insertSelective(nurseRecord);
        if(result > 0){
            //新增成功，判断是否为一般护理类型
            if(!"一般护理类型".equals(nurseRecordAndCostDrugOv.getType())){
                //需要使用药品，添加药品开销单
                DruglistOv druglistOv = new DruglistOv();
                druglistOv.setDrugId(nurseRecordAndCostDrugOv.getDrugId());
                druglistOv.setPrice(nurseRecordAndCostDrugOv.getPrice());
                druglistOv.setNumber(nurseRecordAndCostDrugOv.getNum());
                //计算小计
                BigDecimal total = nurseRecordAndCostDrugOv.getPrice().multiply(new BigDecimal(nurseRecordAndCostDrugOv.getNum()));

                druglistOv.setSubtotal(total);
                druglistOv.setDoctorId(nurseRecordAndCostDrugOv.getDoctorId());
                //查询costID
                int costId = 0;
                try {
                    costId = costMapper.selectByPatientIdAndUnchecked(nurseRecordAndCostDrugOv.getPatientId()).getId();
                }catch (NullPointerException e){
                    //异常，说明没有总费用总费用详情
                    //生成总费用开销表
                    Cost cc = new Cost();
                    cc.setCreateUserId(userId);
                    cc.setPatientId(nurseRecordAndCostDrugOv.getPatientId());
                    costMapper.insertSelective(cc);
                    costId = costMapper.selectByPatientIdAndUnchecked(nurseRecordAndCostDrugOv.getPatientId()).getId();
                }
                druglistOv.setCostId(costId);
                //调用方法添加药品开销单
                int i = costDrugMapper.insertSelective(druglistOv);
                if(i > 0){
                    //添加成功
                    return new ServerResponse(ResponseCode.SUCCESS,"添加药品开销成功");
                }
                //添加失败
                return new ServerResponse(ResponseCode.ERROR,"添加药品开销失败");
            }
            return new ServerResponse(ResponseCode.SUCCESS,"添加护理记录成功！");
        }
        //新增失败
        return new ServerResponse(ResponseCode.ERROR,"添加护理记录失败！");
    }

    //逻辑删除删除护理记录
    public ServerResponse deleteNursingRecord(Integer id){
        NurseRecord nurseRecord = new NurseRecord();
        nurseRecord.setId(id);
        nurseRecord.setIsDeleted(1);
        int result = nurseRecordMapper.updateByPrimaryKeySelective(nurseRecord);
        if(result > 0){
            //删除成功
            return new ServerResponse(ResponseCode.SUCCESS,"删除护理记录成功！");
        }
        //删除失败
        return new ServerResponse(ResponseCode.ERROR,"删除护理记录失败！");
    }

}
